﻿//TODO: check filter, SkinFilter4, but seems to be useful yet, needs some improvement though

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using LowLevelGraphics.ColorSpaces;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// SkinFilter4
    /// </summary>
    public class SkinFilter4 : BaseForeBackgroundColorFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SkinFilter4"/> class.
        /// </summary>
        public SkinFilter4()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SkinFilter4"/> class.
        /// </summary>
        /// <param name="_skinFilter4">The _skin filter4.</param>
        internal SkinFilter4(SkinFilter4 _skinFilter4)
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;
            Color colorToSet = Color.Transparent;

            double fCr = 0.0f;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            Color color = Color.Transparent;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    YCbCr ycbcr = new YCbCr(color);
                    fCr = ycbcr.Cr;
                    if (ycbcr.Cr < 0)
                    {
                        fCr = 0;
                    }
                    bitmap.SetPixel(x, y, Color.FromArgb((int)fCr, (int)fCr, (int)fCr));
                    if (fCr > 10)
                    {
                        //Convert to Positive values
                        fCr = 255;
                        //if (Cr>20)
                        // //Convert to Positive values
                        // Cr = Cr+128;
                        //Set the pixel by cr value
                        bitmap.SetPixel(x, y, Color.FromArgb((int)fCr, (int)fCr, (int)fCr));
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new SkinFilter4(this);
        }
    }
}
